﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using StudentWeb.DAL;
using StudentWeb.Model;
using StudentWeb.Common;
using System.Web;
using System.IO;

namespace StudentWeb.BLL
{
    public class BookBO
    {
        #region 图书信息 分页列表
        /// <summary>
        /// 图书信息 分页列表
        /// </summary>
        /// <param name="pageIndex"></param>
        /// <param name="pageSize"></param>
        /// <param name="wc"></param>
        /// <returns></returns>
        public IResult GetBookList(int pageIndex, int pageSize, string wc = null)
        {
            var list = new BookDataDAO().GetBookList(pageIndex, pageSize, wc);
            return new DataResult<PagedList<dynamic>>(list);
        }
        #endregion

        #region 书籍信息全部记录
        public IResult GetBookList()
        {
            var list = new BookDataDAO().GetList();
            return new DataResult(list);
        }

        #endregion

        #region 图书信息 单条记录
        /// <summary>
        /// 图书信息 单条记录
        /// </summary>
        /// <param name="bookID"></param>
        /// <returns></returns>
        public IResult GetBookInfo(int bookID)
        {
            var res = new BookDataDAO().GetInfo(bookID);
            return new DataResult<BookData>(res);
        }
        #endregion
        #region 图书信息 保存
        public bool SaveBookInfo(dynamic data, int userid)
        {
            int bookID = data.BookID ?? 0;
            var result = 0;
            if (bookID > 0)
            {
                var record = new BookDataDAO().GetInfo(bookID);
                //bookID
                
                record.BookNo = data.BookNo;
                //Bookstate
                record.BookID = data.BookID;
                record.BookName = data.BookName;
                record.BookAuthor = data.BookAuthor;
                record.BookPublisher = data.BookPublisher;
                record.BookPages = data.BookPages;
                record.BookPrice = data.BookPrice;
                record.BookKind = data.BookKind;
                record.BookPublishdate = data.BookPublishdate;
                record.BookImage = data.BookImage ?? "";
                record.EditDateTime = data.EditDateTime;
                record.EditUserID = userid;



                result = new BookDataDAO().Update(record);
            }
            else if (bookID == 0)
            {
                var book = new BookData()
                {
                    BookState = 1,
                    BookNo = data.BookNo,
                    BookName = data.BookName,
                    BookAuthor = data.BookAuthor,
                    BookPublisher = data.BookPublisher,
                    BookPages = data.BookPages,
                    BookKind = data.BookKind,
                    BookPrice = data.BookPrice,
                    BookPublishdate = data.BookPublishdate,
                    BookImage = data.BookImage ?? "",
                    EditDateTime = DateTime.Now,
                    RegDateTime = DateTime.Now,
                    RegUserID = userid,
                    EditUserID = userid,

                };
                result = new BookDataDAO().Insert(book);
            }
            return result > 0;
        }
        #endregion

        #region 图书信息 封面
        public DataResult UploadFile(HttpFileCollection files)
        {
            var res = new DataResult(null, ResultCode.Fail);
            try
            {
                var file = files["file"];
                if (file != null)
                {
                    byte[] buffer;
                    if (file.ContentLength > 1048576 * 10) //10MB
                    {
                        return new DataResult(null, ResultCode.Fail, "文件超出大小限制");
                    }
                    var fileFolder = "/Images/"; //存放图片的文件夹
                    var filePath = HttpContext.Current.Server.MapPath(fileFolder); //存放图片的完整文件夹
                    if (!Directory.Exists(filePath)) //判断文件夹是否存在，如不存在请新创建
                    {
                        Directory.CreateDirectory(filePath);
                    }
                    string fileExt = Path.GetExtension(file.FileName); //取文件扩展名
                    var fileName = Guid.NewGuid().ToString().Replace("-", "") + fileExt; //生成随机文件名
                    var fullPath = Path.Combine(filePath, fileName); //完整路径（文件夹+文件名）
                    file.SaveAs(fullPath); //保存
                    var url = fileFolder + fileName; //图片URL地址
                    res = new DataResult(new { url });
                }
                else
                {
                    res = new DataResult(null, ResultCode.Fail);
                }
            }
            catch (Exception ex)
            {
                res = new DataResult(null, ResultCode.Fail, ex.Message);
            }
            return res;
        }
        #endregion

        #region SetBookState 设置图书状态
        public IResult SetBookState(int bookID, int bookState)
        {
            var record = new BookDataDAO().GetInfo(bookID);
            if (record == null)
            {
                return new Result(ResultCode.Fail, "未找到此ID书");
            }
            int newState = 0;
            if (bookState == 1)
            {
                newState = 2;
            }
            //else if (bookState == 1)
            else if (bookState == 2)
            {
                newState = 1;
            }
            record.BookState = newState;
            var result = new BookDataDAO().Update(record);
                return result >=1 ? new Result() : new Result(ResultCode.Fail, "修改失败");

        }
        #endregion
    }
}
